package com.hmall.common.config.mq;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;

import java.time.Duration;

/**
 * <p>
 * 延迟消息后处理器
 * <p>
 * @author: liuhongjun
 * @since: 2024-05-17
 */
public class DelayedMessageProcessor implements MessagePostProcessor {

    private final long delay;

    public DelayedMessageProcessor(Duration delay) {
        this.delay = delay.toMillis();
    }

    @Override
    public Message postProcessMessage(Message message) {
        // 添加延迟时间
        message.getMessageProperties().setHeader("x-delay", delay);
        return message;
    }
}
